<?php

namespace app\controllers\h5\mini;

use app\controllers\h5\base\H5Controller;
use app\models\News;
use app\models\system\SetApp;
use app\models\system\SetSwiper;
use app\models\system\SetWx;
use app\util\Util;

/**
 * wx mini image
 *
 * Class ImageController
 * @package app\controllers\h5\mini
 */
class ImageController extends H5Controller
{
    public function actionGetMainImage()
    {
        return $this->successEncode([
            'search' => '/mini/search.png',
            'back' => '/mini/back.png',
            'more' => '/mini/more.png',
            'bg' => [
                'home' => '/mini/bg/home.png',
                'search' => '/mini/bg/search.png',
                'user' => '/mini/bg/user.png',
                'center' => '/mini/bg/center.png',
                'c1' => '/mini/c-1.png',
                'c2' => '/mini/c-2.png',
            ],
            'detail' => [
                'home' => '/mini/detail/home.svg',
                'fav' => '/mini/detail/fav.svg',
                'fav-active' => '/mini/detail/fav-active.svg',
                'cart' => '/mini/detail/cart.svg',
                'service' => '/mini/detail/service.svg',
            ],
            'news' => '/mini/news.png',
        ]);
    }

    // news bg
    public function actionGetNewsBg()
    {
        $first_new = News::find()
            ->where(['status' => 1])
            ->andWhere(['is_home' => 1])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            // ->select('title')
            ->asArray()
            ->one();

        $convert = Util::convertImages($first_new['content']);
        $first_new['inImages'] = $convert['images'];
        $first_new['inText'] = $convert['text'];
        unset($first_new['content']);

        // var_dump($first_new);exit;

        // logo
        $set = SetApp::getLastOne();
        $logo = $set->logo;
        if ($logo) {
            $logo = $logo->path;
        }

        return $this->successEncode([
            'news' => '/mini/news.png',
            'first_new' => $first_new,
            'logo' => $logo,
        ]);
    }

    // 微信公众号图片
    public function actionWx()
    {
        $set = SetWx::getLastOne();
        return $this->successEncode(['image' => $set['image']['path']]);
    }

    // 小程序首页
    public function actionHome()
    {
        $swiper = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 1])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->all();

        $middle = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 2])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->limit(3)
            ->asArray()
            ->all();


        return $this->successEncode([
            'swiper' => $swiper,
            'middle' => $middle,
        ]);
    }


    public function actionHomeOld()
    {
        $swiper = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 1])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->all();

        $middle = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 2])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->one();

        $column1_title = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 7])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->one();
        $column1 = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 8])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->all();

        $column2_title = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 9])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->one();
        $column2 = SetSwiper::find()
            ->with(['image'])
            ->where(['status' => 1])
            ->andWhere(['category_id' => 10])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->all();

        return $this->successEncode([
            'swiper' => $swiper,
            'middle' => $middle,
            'column1_title' => $column1_title,
            'column1' => $column1,
            'column2_title' => $column2_title,
            'column2' => $column2,
        ]);
    }
}